/*
 * Copyright 2020-2030 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wwl.stack;

@SuppressWarnings("all")
public class LinkedList<T> {

    private Node<T> dummyHead;
    private int size;

    public LinkedList() {
        dummyHead = new Node<>(null, null);
        size = 0;
    }

    public int size() {
        return size;
    }

    public boolean empty() {
        return size == 0;
    }

    public void addFirst(T t) {
        add(0, t);
    }

    public void add(int index, T t) {
        if (index < 0 || index > size) throw new IllegalArgumentException("Add failed. Illegal index.");
        Node<T> prev = dummyHead;
        for (int i = 0; i < index; i++) prev = prev.next();
        prev.setNext(new Node(t, prev.next()));
        size++;
    }

    public void addLast(T t) {
        add(size, t);
    }

    public T get(int index) {
        if (index < 0 || index >= size) throw new IllegalArgumentException("Get failed. Illegal index.");
        Node<T> cur = dummyHead.next();
        for (int i = 0; i < index; i++) cur = cur.next();
        return cur.data();
    }

    public T getFirst() {
        return get(0);
    }

    public T getLast() {
        return get(size - 1);
    }

    public void set(int index, T t) {
        if (index < 0 || index >= size) throw new IllegalArgumentException("Set failed. Illegal index.");
        Node cur = dummyHead.next();
        for (int i = 0; i < index; i++) cur = cur.next();
        cur.setE(t);
    }

    public boolean contains(T t) {
        for (Node cur = dummyHead.next(); cur != null; cur = cur.next()) if (cur.data().equals(t)) return true;
        return false;
    }

    public T remove(int index) {
        if (index < 0 || index >= size) throw new IllegalArgumentException("Remove failed. Illegal index.");
        Node<T> prev = dummyHead;
        for (int i = 0; i < index; i++) prev = prev.next();
        Node<T> retNode = prev.next();
        prev.setNext(retNode.next());
        retNode.setNext(null);
        size--;
        return retNode.data();
    }

    public T removeFirst() {
        return remove(0);
    }

    public T removeLast() {
        return remove(size - 1);
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        for (Node cur = dummyHead.next(); cur != null; cur = cur.next()) res.append(cur + "->");
        res.append("NULL");
        return res.toString();
    }

}
